package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.WriterException;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.module.mine.contract.QRCodeContract;
import com.meitianhui.convenitenbuy.module.mine.presenter.QRCodePresenter;
import com.meitianhui.convenitenbuy.util.GlideUtils;
import com.meitianhui.convenitenbuy.util.ZXingUtils;

import javax.inject.Inject;

/**
 * Created by amy on 2017/11/6.
 */

public class QRCodeActivity extends BaseToolbarActivity implements QRCodeContract.View {
    private ImageView mIvQrUserPhoto;
    private TextView mTvQrUserName;
    private ImageView mIvQrQrcode;
    @Inject
    QRCodePresenter mQRCodePresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTbTitle("二维码");
        mIvQrUserPhoto = getViewById(R.id.iv_qr_user_photo);
        mTvQrUserName = getViewById(R.id.tv_qr_user_name);
        mIvQrQrcode = getViewById(R.id.iv_qr_qrcode);

        Intent intent = getIntent();
        String userPhoneNumber = intent.getStringExtra("userPhoneNumber");

        createUserQrCode(userPhoneNumber);
        mTvQrUserName.setText(InfoProvider.getConsumerInfo(this).getNick_name());
        GlideUtils.setUserCircleImage(this,InfoProvider.getConsumerInfo(App.getInstance()).getHead_pic_path(), mIvQrUserPhoto);
    }

    //生成用户二维码
    private void createUserQrCode(String userPhoneNumber) {
        DisplayMetrics dm2 = getResources().getDisplayMetrics();
        try {
            mIvQrQrcode.setImageBitmap(ZXingUtils.createQRCode(userPhoneNumber, (int) (200 * dm2.density)));
            Animation a = AnimationUtils.loadAnimation(this, R.anim.zoom_in);
            mIvQrQrcode.setAnimation(a);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected int setView() {
        return R.layout.frament_qr_code;
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    public void showError(String msg) {

    }
}
